﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoChat.Models;

namespace VideoChat.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var cook = Request.Cookies["login"];
            var keyCook = Request.Cookies["key"];

            if (cook != null && keyCook != null && !String.IsNullOrEmpty(keyCook.Value))
            {
                ViewBag.Login = HttpUtility.UrlDecode(cook.Value);
                ViewBag.MyKey = keyCook.Value;
            }

            var data = new Data();
            var model = data.GetUsersStore().GetChatHistory(C.MAIN_CHAT_GROUP).OrderBy(m => m.Date).ToList();

            return View(model);
        }

        [HttpGet]
        public ActionResult GetChatHistory(string roomKey = C.MAIN_CHAT_GROUP)
        {
            var data = new Data();
            var model = data.GetUsersStore().GetChatHistory(roomKey).OrderBy(m => m.Date).ToList();
            return Json(model);
        }
        [HttpPost]
        public ActionResult UpdateRoomState(string roomKey, VideoState state)
        {
            var keyCook = Request.Cookies["key"];
            if (keyCook != null && !String.IsNullOrEmpty(keyCook.Value))
            {
                var data = new Data();
                data.GetUsersStore().CloseRoom(roomKey, keyCook.Value, state);
            }
            return Json(new { success = true });
        }


        [HttpPost]
        public ActionResult Login(string login)
        {
            var errors = new Dictionary<string, string>();
            if (!String.IsNullOrEmpty(login) && login.Length < 255)
            {
                var keyCook = Request.Cookies["key"];
                if (keyCook == null)
                {
                    keyCook = new HttpCookie("key", Guid.NewGuid().ToString());
                    HttpContext.Response.Cookies.Add(keyCook);
                }

                var loginCook = new HttpCookie("login", HttpUtility.UrlEncode(login)) { Expires = DateTime.Now.AddDays(1) };
                HttpContext.Response.Cookies.Add(loginCook);

                return Json(new { success = true, name = login });
            }

            return Json(new { success = false, errors });
        }


        [HttpGet]
        public ActionResult Room(bool isMy, string roomKey)
        {
            var data = new Data();
            var model = data.GetUsersStore().GetRoomInfo(roomKey);

            if (!isMy)
            {
                // Рокируем модель, если она "не наша"
                model = new RoomModel
                        {
                            RoomKey = model.RoomKey,
                            MyKey = model.CalleeKey,
                            MyName = model.CalleeName,
                            MyPublicKey = model.CalleePublicKey,
                            CalleeKey = model.MyKey,
                            CalleeName = model.MyName,
                            CalleePublicKey = model.MyPublicKey
                        };
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult CloseRoom(string roomKey)
        {
            var data = new Data();
            data.GetUsersStore().CloseRoom(roomKey);

            return Json(new { success = true });
        }

        public ActionResult SaveState(string talkId, string userKey, bool isCamera, bool isCameraOn, bool isMicrophone, bool isMicrophoneOn)
        {
            var data = new Data();
            data.GetUsersStore().SaveRoomState(talkId, userKey, isCamera, isCameraOn, isMicrophone, isMicrophoneOn);
            return Content("OK");
        }
    }
}
